var AddressModule;
(function (AddressModule) {
    //地址管理类
    var Address = (function () {
        function Address(oDialog) {
            this.data = {};
            this._selects = 'province city area'.split(' ');
            this.addUrl = ''; //添加地址
            this.saveUrl = ''; //修改地址
            this.setDefaultUrl = ''; //修改默认地址
            this.delUrl = ''; //删除地址
            this.dialog = oDialog;
        }
        /**
         * 弹出地址框
         * @param context any 地址元素环境
         * @param title  弹出框标题
         * @param data addressData 传入数据
         */
        Address.prototype.openDialog = function (context, title, data) {
            if (title === void 0) { title = '地址'; }
            if (this._isOpen)
                return false;
            this._context = context;
            this._empty();
            this._isOpen = true;
            var _this = this;
            if (data) {
                this.data = data;
                this._setInputValueFromData();
            }
            this._initCity();
            this.dilAddress = this.dialog.dialogElem({
                content: this._context,
                title: !$.isEmptyObject(this.data) ? '编辑收货地址' : '新增收货地址',
                cancelValue: '取消',
                cancel: function () {
                    _this._destory();
                },
                okValue: '保存收货地址',
                ok: function () {
                    _this._emptyError();
                    if (_this._inputIsAllFinish()) {
                        _this._saveAddress();
                    }
                    return false;
                }
            });
        };

        /**
         * 销毁数据
         */
        Address.prototype._destory = function () {
            this._isOpen = false;
            this.data = {};
            this._context = null;
            this.dilAddress = null;
        };
        /**
         * 根据data的值对input和select 进行赋值
         */
        Address.prototype._setInputValueFromData = function () {
            for (var key in this.data) {
                if (!~this._selects.indexOf(key))
                    $('.' + key, this._context).val(this.data[key]);
            }
        };
        /**
         * 保存地址
         */
        Address.prototype._saveAddress = function () {
            var url = this.data.id ? this.saveUrl : this.addUrl;
            var _this = this;
            ajax({
                url: url,
                data: this.data,
                showError: true,
                cache: false,
                success: function(data) {
                    if (data.id && data.type == "success") {
                        _this.data.id = data.id;
                        _this.data.data = JSON.stringify(_this.data);
                        _this.addressAction.add(_this.data);
                    } else {
                        _this.data.data = JSON.stringify(_this.data);
                        _this.addressAction.edit(_this.data);
                    }
                    _this.dilAddress.close();
                    _this._destory();
                },
                complete:function(){
                 
                   
                }
            });

        };
        /**
         * 删除地址
         * @param 地址id number
         */
        Address.prototype.delAddress = function (id) {
            var _this = this;
            this.dialog.confirm('确定删除这个地址', function () {
                $.post(_this.delUrl, {id: id}, function(data){}, 'json');
                _this.addressAction.del();
            });
        };
        /**
         * 设置默认地址
         * @param 地址 id
         */
        Address.prototype.setDefault = function (id) {
            $.post(this.setDefaultUrl, { id: id }, function (data) {
            }, 'json');
            this.addressAction.setDefault();
        };
        //清空内荣
        Address.prototype._empty = function () {
            this._emptyInput();
            this._emptyError();
        };
        //清空输入框
        Address.prototype._emptyInput = function () {
            this._context.find('input').val(''); //清空输入框
        };
        //清空错误
        Address.prototype._emptyError = function () {
            this._context.find('.error-msg').text(''); //清空错误
        };
        /**
         * 设置输入数控
         * @return {addressData}
         */
        Address.prototype._setData = function () {
            var pro = $('select.prov', this._context).val(), city = $('select.city', this._context).val(), dist = $('select.dist', this._context).val(), pC = $('select.prov option[value=' + pro + ']', this._context).text(), cC = $('select.city option[value=' + city + ']', this._context).text(), dC = $('select.dist option[value=' + dist + ']', this._context).text();
            var data = {
                name: $.trim($('.name', this._context).val()),
                prov: pro + ',' + city + ',' + dist,
                category: pC + cC + dC,
                address: $.trim($('.address', this._context).val()),
                areaTree:  pro + ',' + city + ',' + dist,
                phone: $('.phone', this._context).val(),
                zipCode:$('.zipCode', this._context).val(),
                alias:$('.alias', this._context).val(),
                contact: $('.phone', this._context).val(),
                id: $('.id', this._context).val()
            };
            return this.data = data;
        };
        //判断输入是否完成
        Address.prototype._inputIsAllFinish = function () {
            var data, isSelectCity;
            data = this._setData();
            if (!data.name.length) {
                $('.name').next('.error-msg').text('请您填写收货人姓名').end().focus();
                return false;
            }
            if (data.name.length > 25) {
                 $('.name').next('.error-msg').text('收货热姓名不能大于25位').end().focus();
                  return false;
            }
            isSelectCity = true;
            $('#element_id select:visible').each(function (i, o) {
                if (!$(o).val()) {
                    return isSelectCity = false;
                }
            });
            if (!isSelectCity) {
                $('#element_id .error-msg').text('请选择所在地区');
                return false;
            }
            if (!data.address.length) {
                $('.address').next('.error-msg').text('请填写详细的地址').end().focus();
                return false;
            }
            if(data.address.length > 50) {
            	$('.address').next('.error-msg').text('收货人详细地址过长').end().focus();
                return false;
            }
            if (!/^(13\d|145|147|15[0-3]|15[5-9]|177|18[0-3]|18[5-9])\d{8}$/.test(data.phone)) {
                $('.phone').next('.error-msg').text('请填写正确的手机号码').end().focus();
                return false;
            }
//            if(!/^[1-9][0-9]{5}$/.test(data.zipCode)){
//                $('.zipCode').next('.error-msg').text('请填写正确的邮编').end().focus();
//                return false;
//            }
            return true;
        };
        //初始化城市联几
        Address.prototype._initCity = function () {
            var _this = this, cS = '929 956 957'.split(' ');
            if (this.data && this.data.prov) {
                var p = this.data.prov.replace(/^,|,$/, '').split(',');
                cS = p;
            }
            if (cS.length == 2)
                cS[2] = '';
            $('#element_id', this._context).citySelect({
                url: _this.cityUrl,
                prov: cS[0],
                city: cS[1],
                dist: cS[2]
            });
        };
        return Address;
    })();
    AddressModule.Address = Address;
})(AddressModule || (AddressModule = {}));
